import {
  cancelOrder,
  deleteOrder,
  followOrUnfollowApi,
  getPrescriptionPicApi
} from '@/services/consult'
import { sendMobileCode } from '@/services/user'
import { onMounted, onUnmounted, Ref, ref } from 'vue'
import { ConsultOrderItem, FollowType } from '@/types/consult'
import {
  FormInstance,
  showFailToast,
  showImagePreview,
  showSuccessToast,
  showToast
} from 'vant'
import { OrderType } from '@/enums'
import { OrderDetail } from '@/types/order'
import { getMedicalOrderDetail } from '@/services/order'
import { CodeType } from '@/types/user'

export function useFollow(type: FollowType) {
  const loading = ref(false)
  const followOrUnfollow = async (item: { id: string; likeFlag: number }) => {
    try {
      loading.value = true
      await followOrUnfollowApi(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return { loading, followOrUnfollow }
}

export const useOnShowPrescription = () => {
  const onShowPrescription = async (id: string) => {
    if (id) {
      const res = await getPrescriptionPicApi(id)
      showImagePreview([res.data.url])
    }
  }
  return {
    onShowPrescription
  }
}

// 封装取消订单逻辑
export const useCancelOrder = () => {
  const loading = ref(false)
  const cancelConsultOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      await cancelOrder(item.id)
      item.status = OrderType.ConsultCancel
      item.statusValue = '已取消'
      showSuccessToast('取消成功')
    } catch (error) {
      showFailToast('取消失败')
    } finally {
      loading.value = false
    }
  }
  return { loading, cancelConsultOrder }
}

// 删除订单逻辑
export const useDeleteOrder = (cb: () => void) => {
  const deleteLoading = ref(false)
  const deleteConsultOrder = async (item: ConsultOrderItem) => {
    try {
      deleteLoading.value = true
      await deleteOrder(item.id)
      showSuccessToast('删除成功')
      cb && cb()
    } catch (error) {
      showFailToast('删除失败')
    } finally {
      deleteLoading.value = false
    }
  }
  return {
    deleteLoading,
    deleteConsultOrder
  }
}

/**
 * 订单详情
 * @param id
 */
export const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return { order, loading }
}

// 发送短信验证码吗逻辑
export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  const form = ref<FormInstance>()
  const time = ref(0)
  let timeId: number
  const onSend = async () => {
    if (time.value > 0) return
    await form.value?.validate('mobile')
    await sendMobileCode(mobile.value, type)
    showToast('发送成功')
    time.value = 60
    // 倒计时
    clearInterval(timeId)
    timeId = setInterval(() => {
      time.value--
      if (time.value <= 0) clearInterval(timeId)
    }, 1000)
  }
  onUnmounted(() => {
    clearInterval(timeId)
  })
  return { form, time, onSend }
}
